package  com.storage.view.widgets.form
{
	import com.storage.util.LoadXmlUtil;
	import com.storage.view.widgets.grid.ZZGrid;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	import flash.utils.describeType;
	
	import flexlib.containers.FlowBox;
	
	import mx.collections.ArrayCollection;
	import mx.containers.FormItem;
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.ComboBox;
	import mx.controls.DateField;
	import mx.controls.Label;
	import mx.controls.RadioButton;
	import mx.controls.Spacer;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.ValidationResultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.validators.Validator;

	//[Event(name="change",type="mx.events.Event")]			//form to gried 
	[Event(name="valueChanged",type="mx.events.Event")]		//value has changed
	[Event(name="getComboBoxItem",type="mx.events.Event")]	//send value to comobox's value of form
	[Event(name="saveOrUpdate",type="mx.events.Event")]		//提交事件
	[Event(name="bindData",type="mx.events.Event")]			//绑定数据事件
	[Event(name="insertNewRecord",type="mx.events.Event")]	//插入新记录事件
	[Event(name="change", type="mx.events.Event")]			//变化事件
	
	public class ZZEditForm extends VBox
	{
		[Bindable]
		private var _bindObject:Object;						//绑定实体的变量
		
		[Bindable]
		private var _objectList:Array;
		
		private var _readOnly:Boolean;
		
		public var saveOrUpdateHandler:Function;
		
		
		private var _listButtonBox:HBox;
		private var _labelText:String;
		
		private var _hasAddListControlList:Boolean = false;
		
		[Bindable]
		public var firstControlName:String;
		
		
		//----------------------------------------------------------------------
		private var _btnList:ArrayCollection = new ArrayCollection();		//用于储存所有按钮
		private var _subBtn:Button;											//提交按钮
		private var _resetBtn:Button;										//重置按钮
		private var _cancelBtn:Button;										//取消按钮
		private var _btnBox:HBox;											//按钮容器
		//----------------------------------------------------------------------
		
		
		//----------------------------------------------------------------------
		public var CONTROLLERWIDTH:Number = 130;						//每个控件间的长度
		public var LABWIDTH:Number = 60;								//每个label的长度

		private var _FLOWBOXHEIGHT:Number = 270;						//flowbox的高度
		public var HORGAP:Number = 20;									//每列控件的间隙
		public var LISTWIDTH:Number = 120;
		private var _CHILDVBOXHEIGHT:Number = 5;
		//----------------------------------------------------------------------
		
		
		//----------------------------------------------------------------------------
		public var controlBox:HBox;										//控件容器
		public var flowBox:FlowBox;										//盛装控件容器		
		public var controlList:ArrayCollection = new ArrayCollection(); //用于储存表中所有的表单空间															
		public var controlDic:Dictionary = new Dictionary();			//将控件根据其名称储存进dec内
		//----------------------------------------------------------------------------
		
		
		//----------------------------------------------------------------------------
		private var _voClass:Class;										//表单所绑定的实体名称
		private var _xmlData:XMLList;									//表单控件xml集合
		private var _xmlURL:String;										//load xml url
		//----------------------------------------------------------------------------
		
		//----------------------------------------------------------------------------
		private var _childGridContainer:ChildTabNavigator;		//若是主从表，则子表的datagrid放在这里面
		private var _vbChildBox:VBox ;
		//----------------------------------------------------------------------------
		
		
		//----------------------------------------------------------------------------
		private var _comboxList:ArrayCollection = new ArrayCollection;
		private var _comboBoxDataDic:Dictionary							//装载comboBoxDataProvider
		//----------------------------------------------------------------------------
		
		//----------------------------------------------------------------------------
		public var validationList:ArrayCollection = new ArrayCollection();//validate list
		private var valueChangedDic:Dictionary = new Dictionary();
		private var _valueChanged:Boolean;					//表单内的值是否有改变
		//----------------------------------------------------------------------------
		
		
		public static var DELETE:Number =  8;
		public static var MODIFY:Number = 16;
		public static var ADD:Number = 4;
		
		public var sameGradeGrid:ZZGrid;
		public var childGrid:ZZGrid;
		public var childForm:ZZEditForm;
		
		
		/**构造函数
		 * @param Url:加载xml的url
		 * @param voName:form捆绑的实体名
		 * **/
		public function ZZEditForm(Url:String=null,voClass:Class=null){
			super();
			setStyle("horizontalAlign","center");
			setStyle("verticalAlign","middle");
			setStyle("paddingLeft",8);
			percentHeight =100;
			percentWidth =100;
			
			
			//create viewstack
			//formViewStack = new ViewStack();
			//formViewStack.percentWidth = 100;
			//formViewStack.percentHeight = 90;
			
			if(Url!=null){	//当url不为空的时候加载xml配置文件
				this._xmlURL = Url;
				loadXmlForm(Url);
			}
			if(voClass != null){//当voname不为空的时候，实例化bindObject
				this._voClass = voClass;
				
			}
			createFlowBox();
			createButtons();
		}
		
		/**
		 * 加载xml
		 * **/
		private function loadXmlForm(url:String):void{
			LoadXmlUtil.loadXml(_xmlURL,loadXmlResultHandler);
		}
		/***
		 * 重写 createChildren方法
		 * **/
		override protected function createChildren():void{
			super.createChildren();
			//若xmldata不为空的时候才建造表单控件
			if(this._xmlData != null){
				percentWidth = 100;
				initEvents();
				createCmp();	
			}
		}
		
		/**
		 * 注册事件
		 * **/
		private function initEvents():void{
			this.addEventListener(FlexEvent.CREATION_COMPLETE,bindDataHandler);
			//用于grid to form时需要将combobox中的实体load出来
			this.addEventListener("getComboBoxItem",getComboBoxItemHandler);
			//用于grid to form时触发的事件，让选中的object值传到form里的控件中
			this.addEventListener("bindData",bindDataHandler);
			//当控件内的之改变时触发改事件
			this.addEventListener("valueChanged",valueChangedHandler);
			//
			this.addEventListener("insertNewRecord",insertNewRecordHandler);
			//
			this.addEventListener("saveOrUpdate" ,saveOrUpdate);
			//addEventListener(Event.ADDED_TO_STAGE,bindDataHandler);
		}
		
		/**
		 * 创建容器和控件
		 * **/
		private function createCmp():void{
			
			createControllers();
			
		}
		
		/***
		 * 创建外层的hbox和flowbox容器
		 * **/
		private function createFlowBox():void{
			
			//初始化最外层的hbox
			controlBox = new HBox();
			//controlBox.height = 280;
			controlBox.percentWidth = 100;
			controlBox.percentHeight = 100;
			controlBox.setStyle("horizontalAlign","center");
			controlBox.setStyle("verticalAlign","top");
			controlBox.setStyle("paddingBottom",8);
			controlBox.setStyle("paddingLeft",4);
			controlBox.horizontalScrollPolicy = "off";
			controlBox.verticalScrollPolicy  = "off";
			//controlBox.setStyle("paddingTop",8);
			controlBox.setStyle("paddingRight",4);
			//controlBox.setStyle("borderStyle","inset");
			//controlBox.setStyle("borderColor","silver");
			//初始化hbox内盛装控件的flowbox
			flowBox = new FlowBox();
			flowBox.percentWidth = 98;
			flowBox.height = _FLOWBOXHEIGHT;
			flowBox.autoLayout = true;
			//flowBox.setStyle("paddingTop",8);

			controlBox.addChild(flowBox);

			this.addChild(controlBox);

			_vbChildBox = new VBox();
			_vbChildBox.height = _CHILDVBOXHEIGHT;
			_vbChildBox.percentWidth = 100;
			_vbChildBox.setStyle("horizontalAlign","center");
			_vbChildBox.setStyle("verticalAlign","middle");
			_vbChildBox.horizontalScrollPolicy = "off";
			addChild(_vbChildBox);
		}
		
		//create the child gird hbox
		private function createChildGridBox():void{
			this._childGridContainer = new ChildTabNavigator();
//			_childGridContainer.percentHeight = 100;
//			_childGridContainer.percentWidth = 100;
//			_childGridContainer.setStyle("horizontalAlign","center");
//			_childGridContainer.setStyle("verticalAlign","middle");
			_vbChildBox.addChild(_childGridContainer);
		}
		/**
		 * 创建按钮
		 * **/
		private function createButtons():void{
			//初始化按钮容器
			_btnBox = new HBox();
			_btnBox.percentWidth = 100;
			_btnBox.setStyle("horizontalAlign","center");
			//提交按钮
			_subBtn = new Button();
			_subBtn.label = "确认";
			_subBtn.enabled = false;

			//重置按钮
			_resetBtn = new Button();
			_resetBtn.label = "重置";

			//取消按钮
			_cancelBtn = new Button();
			_cancelBtn.label ="取消";

			//将button添加进容器中
			_btnBox.addChild(_subBtn);
			_btnBox.addChild(_resetBtn);
			_btnBox.addChild(_cancelBtn);
			this.addChild(_btnBox);
			//添加进记录btn的list中
			_btnList.addItem(_subBtn);
			_btnList.addItem(_resetBtn);
			_btnList.addItem(_cancelBtn);
			
			addButsListenner();
//			if(_hasAddListControlList===false && _objectList!==null && _objectList.length>0){
//				createListButtns(_objectList);
//				_hasAddListControlList = true;
//			}
			
		}
		private function addButsListenner():void{
			_subBtn.addEventListener(MouseEvent.CLICK,submitHandler);
			_resetBtn.addEventListener(MouseEvent.CLICK,resetHandler);
			_cancelBtn.addEventListener(MouseEvent.CLICK,cancelHanlder);
		}
		
		/**
		 * 创建表单控件
		 * **/
		private function createControllers():void{
			var len:int = _xmlData.length();
			if(_bindObject===null)
				_bindObject = new _voClass();
				
			firstControlName = xmlData[0].@id;
			
			for(var i:int;i<len;i++){
				var type:String = _xmlData[i].@type;
				var control:UIComponent;
				
				var fitem:FormItem = new FormItem();
				fitem.setStyle("labelWidth",LABWIDTH);
				fitem.width = LABWIDTH + CONTROLLERWIDTH + 20;
				fitem.label = _xmlData[i].@label;
			
				//根据type创建表单组件
				if(type=="textInput"){
					control = createTextInput(_xmlData[i]);
					control.addEventListener("focusOut",changeHandler);
				}else if(type=="comboBox"){
					control = createComboBox(_xmlData[i]);
					control.addEventListener(Event.CHANGE,changeHandler);
				}else if(type=="dateField"){
					control = createDateField(_xmlData[i]);
					control.addEventListener(Event.CHANGE,changeHandler);
				}else if(type=="textArea"){
					control = createTextArea(_xmlData[i]);
					control.addEventListener("focusOut",changeHandler);
				}else if(type=="numberField"){
					control = createNumberField(_xmlData[i]);
					control.addEventListener(Event.CHANGE,changeHandler);
				}
				else if(type=="checkBox"){
					control = createCheckBox(_xmlData[i]);
					control.addEventListener(Event.CHANGE,changeHandler);
				}else if(type=="radio"){//radio做特殊处理
					var xml:XML = _xmlData[i];
					var gName:String = _xmlData[i].@groupName;
					var children:XMLList = _xmlData[i].label;
					//flowBox.addChild(lab);
					for(var k:int=1;k<children.length()+1;k++){
						control = createRadio(children[k-1],gName);
						fitem.addChild(control);

					}
				}else if(type=="child"){
					//control = createChildTabNav(_xmlData[i]);
					return ;
				}
				fitem.addChild(control);
				
				//装入flowbox容器内 radio 因为布局关系
				if(control != null && !(control is RadioButton)){
					flowBox.addChild(fitem);
					
				}
				//因为radio是一组指向一个属性，所以要分别处理
				if(control != null && !(control is RadioButton)){
					//装入dic内
					controlDic[control.id] = control;
					//装入ay内
					controlList.addItem(control);
				}else if(control is RadioButton){//按照radio的groupName记录
					controlDic[RadioButton(control).groupName] = control;
					controlList.addItem(control);
				}
			}
			
		}

		
		//创建textInput控件
		private function createTextInput(xml:XML):TextInput{
			var text:TextInput = new TextInput();
			text.id = xml.@id;
			text.width = CONTROLLERWIDTH;
			if(xml.@requerid==true){
				createReqValidate(text,'text',xml.@label);
			}
			//if(_bindObject!=null){
				text.text = _bindObject[xml.@id];
			//}
			if(xml.@readOnly==true){
				text.enabled = false;
			}
			return text;
		}
		//创建numberFiled控件
		private function createNumberField(xml:XML):NumberField{
			
			var nf:NumberField = new NumberField();
			nf.id = xml.@id;
			nf.width = CONTROLLERWIDTH;
			if(xml.@readOnly==true){
				nf.enabled = false;
			}
			if(xml.@numberOnly==true){
				nf.type = "int";
			}
			if(xml.@requerid==true){
				createReqValidate(nf,'text',xml.@label);
			}
			nf.text = _bindObject[xml.@id];
			return nf;
		}
		
		//创建comboBox控件
		private function createComboBox(xml:XML):ZZComBox{
			var combo :ZZComBox = new ZZComBox();
			combo.id = xml.@id;
			combo.width = CONTROLLERWIDTH;
			var index:int = -1;
			//处理实体关联的combobox
			var pro:String = xml.@dataProvider;
			if(pro!=null && pro!=''){
				combo.dataProviderName = pro;
				combo.queryObject = xml.@entity;
			}else if(xml.@useLabel==true){
					combo.useLabel = true;
					var labList:XMLList = xml.item;
					var ary:ArrayCollection = new ArrayCollection();
					for(var i:int=0;i<labList.length();i++){
						var obj:Object = new Object();
						obj['name'] = labList[i].@name[0];
						obj['value'] = labList[i].@value[0];
						ary.addItem(obj);
					}
					combo.dataProvider = ary;
				
			}
			if(xml.@requerid==true){
				combo.selectedIndex = 0;
				createReqValidate(combo,'selectedItem',xml.@label);
			}else{
				combo.prompt = " ";
				combo.selectedIndex = index;
			}
			combo.labelField = xml.@fieldName;
			//将表单内的comboBox放入list中
			this._comboxList.addItem(combo);
			return combo; 
		}
		//创建dateFiel控件
		private function createDateField(xml:XML):DateField{
			var df:DateField = new DateField();
			df.id = xml.@id;
			df.formatString = "YYYY-MM-DD";
			df.width = CONTROLLERWIDTH;
			//df.selectedDate = _bindObject[xml.@id];
			if(xml.@requerid==true){
				createReqValidate(df,'selectedDate',xml.@label);
			}
			df.selectedDate = _bindObject[xml.@id];
			if(xml.@readOnly==true){
				df.enabled = false;
			}
			return df;
		}
		//创建textArea控件
		private function createTextArea(xml:XML):TextArea{
			var ta:TextArea = new TextArea();
			ta.id = xml.@id;
			ta.width = CONTROLLERWIDTH;
			ta.text = _bindObject[xml.@id];
			if(xml.@requerid==true){
				createReqValidate(ta,'text',xml.@label);
			}
			if(xml.@readOnly==true){
				ta.enabled = false;
			}
			return ta;
		}
		
		//创建checkbox控件
		private function createCheckBox(xml:XML):CheckBox{
			var cb:CheckBox = new CheckBox();
			cb.label = xml.@label;
			cb.width = CONTROLLERWIDTH;
			if(xml.@requerid==true){
				cb.selected = true;
			}
			return cb;
		}
		
		//创建radio控件组
		private function createRadio(xml:XML,groupName:String):RadioButton{
			var radio:RadioButton = new RadioButton();
			radio.label = xml.@name;
			radio.groupName = groupName;
			radio.value = xml.@value;
			radio.width = (CONTROLLERWIDTH)/2;
			return radio;
		}
		
		//创建验证器
		private function createReqValidate(target:UIComponent,property:String,label:String):void{
			var validate:Validator = new Validator();
			validate.required = true;
			validate.source = target;
			validate.property = property;
			validate.requiredFieldError = label+" 不能为空";
			//flowBox.addChild(validate);
			validationList.addItem(validate);
		}
		
		//当grid多选的时候需要创建控制按钮组件
		private function createListButtns(value:Array):void{
			if(_listButtonBox ===null){
				_listButtonBox = new HBox();
				//_listButtonBox.percentWidth = 40;
				_listButtonBox.setStyle("horizontalAlign","right");
				var la:Label = new Label();
				_labelText = "您选择了"+value.length+"条记录";
				la.text = _labelText;
				var forword:Button = new Button();
				forword.label="下一个";
				var back:Button = new Button();
				back.label = "上一个";
				var listCombobox:ComboBox = new ComboBox();
				listCombobox.dataProvider = new ArrayCollection(value);
				listCombobox.labelField = firstControlName;
				listCombobox.width = 100;
				listCombobox.setStyle("textAlign","center");
				var check:Label = new Label();
				check.text = "所选记录";
				_listButtonBox.addChild(la);
				_listButtonBox.addChild(forword);
				_listButtonBox.addChild(back);
				_listButtonBox.addChild(check);
				_listButtonBox.addChild(listCombobox);
				initListButtonsEvents(forword,back,listCombobox);
			}
			_btnBox.addChild(_listButtonBox);
		}
		
		private function initListButtonsEvents(f:Button,b:Button,c:ComboBox):void{
			f.addEventListener(MouseEvent.CLICK,goForwordEntity);
			b.addEventListener(MouseEvent.CLICK,goBackwordEntity);
			c.addEventListener(Event.CHANGE,editListBoxChange);
		}
		
////////////////--------------------事件处理--开始--------------////////////////////////

		
		private function saveOrUpdate():void{
			saveOrUpdateHandler.apply(this,[this._bindObject]);
		}
		// * 处理加载xml成功的函数
		private function loadXmlResultHandler(e:ResultEvent):void{
				_xmlData = e.result.item;
		}
		
		//* 根据bindObject赋值 处理binddata事件的函数
		private function bindDataHandler(e:Event):void{

			if(_bindObject.rowStatus==4){
				return;
			}
			for(var i:int=0;i<controlList.length;i++){
				var cmp:Object = controlList[i];
				
				if(cmp is TextInput){
					TextInput(cmp).text = _bindObject[cmp.id];
				}else if(cmp is DateField){
					DateField(cmp).selectedDate = _bindObject[cmp.id] as Date;
				}else if(cmp is TextArea){
					TextArea(cmp).text =  _bindObject[cmp.id];
				}else if(cmp is NumberField){
					NumberField(cmp).text = _bindObject[cmp.id];
				}else if(cmp is RadioButton){
					
				}else if(cmp is CheckBox){
					
				}else if (cmp is ComboBox || cmp is ZZComBox){
					var index:int;
					if(_bindObject[cmp.id] is String){ //如果非实体
						var ary:Object = cmp.dataProvider ;
						if(ary is Array || ary is ArrayCollection){
							for (var k:int=0;k<ary.length;k++){
								if(ary[k].value==_bindObject[cmp.id]){
									index = k;
									break;						
								}
							}
						}
					}else if(_bindObject[cmp.id] != null){//如果是实体
						var obj:Object = _bindObject[cmp.id] ;
						var pro:String = cmp.labelField;
						if(obj[pro]!=null){
							var list:ArrayCollection = ArrayCollection(cmp.dataProvider);
							index = getComboxIndexByItem(list,obj);
						}
					}
					
					ComboBox(cmp).selectedIndex = index;
					
				}
			}
			getFirstFocus();
			
		}
		
		// * 处理控件change事件的函数
		public function changeHandler(event:Event):void{
			var Idname:String;
			var flag:Boolean = false;
			var target:Object = event.currentTarget;
			if( _bindObject != null){
				if(target is TextInput){
					Idname = TextInput(target).id;
					if(_bindObject[Idname]!=target.text){
						flag = true;
					}
				}else if (target is TextArea){
					Idname = TextArea( target).id;
					if(_bindObject[Idname]!=target.text){
						flag = true;
					}
					
				}else if(target is ComboBox || target is ZZComBox){
					//TODO---PROBLEM
					Idname = ComboBox(target).id;
					var oldc:Object = _bindObject[Idname];
					var newc:Object = ComboBox(target).selectedItem;
					var b:Boolean = equals(oldc,newc);
					if(b==false){
						flag = true;
					}
				}else if(target is DateField){
					Idname = DateField(target).id;
				}else if(target is NumberField){
					Idname = NumberField(target).id;
					if(_bindObject[Idname]!=target.text){
						flag = true;
					}
				}else if(target is DateField){
					var oldd:Date = _bindObject[Idname];
					var newd:Date = DateField(target).selectedDate;
					if(newd != null){
						if(oldd.toDateString() != newd.toDateString()){
							flag = true;
						}
					}
				}
				valueChangedDic[Idname] = flag;
				this.dispatchEvent(new Event("valueChanged"));
			}else{
				_valueChanged = true;
				_subBtn.enabled  = getValidationRslt();
				
			}
		}
		
		 //* 当值发生变化时处理函数
		private function valueChangedHandler(e:Event):void{
			var len :int = controlList.length;
			var flag :Boolean = false;
			for(var i:int=0;i<len;i++){
				var idname:String = controlList[i].id;
				if(valueChangedDic[idname]){
					flag = true;
				}
			}
			_valueChanged = flag;
			var validate:Boolean = getValidationRslt();
			if(flag && validate){
				_subBtn.enabled = flag;
			}else{
				_subBtn.enabled = false;
			}
			

		}
		//插入一条记录事件
		private function insertNewRecordHandler(e:Event):void{
			//TODO
//			var len:int = _comboxList.length;
//			for(var i:int;i<len;i++){
//				ComboBox(_comboxList[i]).selectedIndex = -1;
//			}
			
		}
		
		//处理按钮submit事件
		public function submitHandler(e:MouseEvent):void{
			if(_valueChanged){
				_bindObject = getChangeValueObject();
				saveOrUpdate();
				//cancelHanlder(null);
				dispatchEvent( new Event("change") );
			}
		}
		

		
		//处理按钮reset事件
		public function resetHandler(e:MouseEvent):void{
			for(var i:int=0;i<controlList.length;i++){
				var obj:Object = controlList[i];
				if(obj is TextInput || obj is TextArea){
					obj.text = "";
				}else if(obj is DateField){
					DateField(obj).selectedDate = null;
					DateField(obj).text = "";
				}else if(obj is ZZComBox){
					ZZComBox(obj).selectedIndex = -1;
				}
				
			}
		}
		//处理按钮cancel事件
		public function cancelHanlder(e:MouseEvent):void{
			_subBtn.enabled = false;
			if(_bindObject!=null){
				_bindObject.rowStatus = 2;
			}
			//批量修改时出现的button box
			if(_listButtonBox!=null){
				var bay:Array = _btnBox.getChildren();
				for each(var obj:DisplayObject in bay){
					if(obj===_listButtonBox){
						_btnBox.removeChild(_listButtonBox);
					}
				}
			}
			//触发表单旋转效果
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		//处理combobox获取实体事件的函数
		private  function getComboBoxItemHandler(e:Event=null,value:Dictionary=null):void{
			
			_comboBoxDataDic = value;	
			if(_comboBoxDataDic==null)
				return ;
			var len:int = _comboxList.length;
			for(var i:int;i<len;i++){
				//设置comboBox内的dataprovider
				var pro:String = ZZComBox(_comboxList[i]).dataProviderName ;
				if(pro!='' && pro!=null){
					var obj:Object = _comboBoxDataDic[pro];
					ComboBox(_comboxList[i]).dataProvider = ArrayCollection(_comboBoxDataDic[pro]);
				}		
				
			}
			
		}
		
		
		
		
		
		
		
		private function goForwordEntity(e:MouseEvent):void{
			
		}
		private function goBackwordEntity(e:MouseEvent):void{
			
		}
		private function editListBoxChange(e:Event):void{
			var cob:ComboBox = ComboBox(e.currentTarget);
			bindObject = cob.selectedItem ;
		}
		


////////////////--------------------事件处理--结束--------------////////////////////////
	

///////////////////////-----------get&set---begin------------//////////////////////////	
		//验证所有表单是否通过
		private function getValidationRslt():Boolean{
			var flag:Boolean = true;
			var vary:Array = Validator.validateAll(validationList.source);
			for(var k:int=0;k<vary.length;k++){
				var vr:ValidationResultEvent = vary[k];
				if(vr.type==ValidationResultEvent.INVALID){
					flag = false;
				}
			}
			return flag;
		}
		
		
		public function set xmlData(xmlList:XMLList):void{
			this._xmlData = xmlList;
			//xmlDate赋值之后再创建组件（为空的时候未创建）
			createChildren();
		}
		
		private function getChangeValueObject():Object{
			//TODO 	rowStatus 
			var len:int = controlList.length;
			if(_bindObject==null)
				_bindObject = new _voClass();
			for(var i:int=0;i<len;i++){
				var ct:Object = controlList[i];
				var pro:String = ct.id;
				
				if(ct is TextInput || ct is TextArea || ct is NumberField){
					_bindObject[pro] = ct.text;
				}else if(ct is DateField){
					_bindObject[pro] = DateField(ct).selectedDate;
				}else if(ct is CheckBox){
					if(CheckBox(ct).selected){
						
					}
				}else if(ct is RadioButton){
					var p:String = RadioButton(ct).groupName;
					if(RadioButton(ct).selected){
						_bindObject[p] = RadioButton(ct).value;
					}
				}else if(ct is ZZComBox  ){
					if(ZZComBox(ct).useLabel){
						_bindObject[pro] = ZZComBox(ct).selectedItem.value;
					}else{
						_bindObject[pro] = ComboBox(ct).selectedItem;
					}
					
				}
				
			}
			//if(_bindObject.rowStatus != 16)
				//_bindObject.rowStatus = 4;
			
			return _bindObject;
		}
		
		
		public function getFirstFocus():void{
			if(controlList[0] !=null && controlList[0] is TextInput)
				TextInput(controlList[0]).setFocus();
		}
		//从外部添加controllers
		public function set controllers(ar:Array):void{
			for(var i:int=0;i<ar.length;i++){
				addControlerListener(ar[i] as DisplayObject);
				var diso:DisplayObject  = ar[i];
				diso.width = CONTROLLERWIDTH;
				var lb:Label = new Label();
				lb.text = diso.name;
				lb.width = LABWIDTH;
				var space:Spacer  = new Spacer();
				space.width = HORGAP;
				flowBox.addChild(lb);
				flowBox.addChild(diso);
				flowBox.addChild(space);
				controlList.addItem(diso);
			}
		}		
				private function addControlerListener(obj:DisplayObject):void{
						obj.addEventListener(Event.CHANGE,changeHandler);
				}
		
		public function set buttonList(ar:Array):void{
			this._subBtn = ar[0];
			this._resetBtn = ar[1];
			this._cancelBtn = ar[2];
			this._btnList.source = ar;
			addButsListenner();
		}
		public function get buttonList():Array{
			return this._btnList.source;
		}
		
		public function set valueChanged(value:Boolean):void{
			this._valueChanged = value;
			_subBtn.enabled = value;
			//this._bindObject.rowStatus = MODIFY;
		}
		public function get valueChanged():Boolean{
			return this._valueChanged;
			
		}
		
		
		public function get readOnly():Boolean{
			return this._readOnly;
		}
		public function  set readOnly(value:Boolean):void{
			this._readOnly = value;
			var len:Number = _btnList.length;
			for(var i:Number=0;i<len;i++){
				var b:Button = _btnList[i] as Button;
				if(b.label=='取消'){
					b.visible = true;
				}else{
					b.visible = false;
				}
			}
		}
		
		
		//提交按钮
		public function get subButton():Button{
			return this._subBtn;
		}
		public function  set subtButton(b:Button):void{
			this._subBtn = b;
		}
		//重置按钮
		public function get resetButton():Button{
			return this._resetBtn;
		}
		public function  set resetButton(b:Button):void{
			this._resetBtn = b;
		}
		//取消按钮
		public function get cancelButton():Button{
			return this._cancelBtn;
		}
		public function  set cancelButton(b:Button):void{
			this._cancelBtn = b;
		}
		
		//获取表单中combobox的值
		[Bindable]
		public function set comboBoxDataDic(dic:Dictionary):void{
			_comboBoxDataDic = dic;
			getComboBoxItemHandler(null);
		}
		public function get comboBoxDataDic():Dictionary{
			return _comboBoxDataDic;
		}
		//获取xml数据
		public function get xmlData():XMLList{
			return this._xmlData;
		}
		//编辑实体名称
		public function set voClass(name:Class):void{
			this._voClass = name;
		}
		public function get voClass():Class{
			return this._voClass;
		}
		//xml的url
		public function get xmlURL():String{
			return this._xmlURL;
		}
		public function set xmlURL(xmlurl:String):void{
			this._xmlURL = xmlurl;
		}
		
		//编辑所绑定的实体
		[Bindable]
		public function get bindObject():Object{
			
			return this._bindObject;
		}
		
		public function set bindObject(value:Object):void{
			this._bindObject = value;
			//dispatchEvent(new Event("bindData"));
		}
		
		//实体集合
		[Bindable]
		public function get objectList():Array{
			return _objectList;
		}
		public function set objectList(value:Array):void{
			if(value!=null && value.length > 0){
				createListButtns(value);
				//_hasAddListControlList = true;
			}
			_objectList = value;
			
		}
		
//		public function set childGridContainer(value:ChildTabNavigator):void{
//			this._childGridContainer = value;
//		}
		
		public function get childGridContainer():ChildTabNavigator{
			if(_childGridContainer==null){
				createChildGridBox();
			}
			return this._childGridContainer;
		}
		public function get FLOWBOXHEIGHT():Number{
			return  this._FLOWBOXHEIGHT
		}
		public function set FLOWBOXHEIGHT(value:Number):void{
			this._FLOWBOXHEIGHT = value;
			this.flowBox.height = value;	
		}
	
		public function get CHILDVBOXHEIGHT():Number{
			return  this._CHILDVBOXHEIGHT ;	
		}
		public function set CHILDVBOXHEIGHT(value:Number):void{
			this._CHILDVBOXHEIGHT = value;
			this._vbChildBox.height = value;
			
		}
		
///////////////////////-----------get&set---end--------------//////////////////////////	
		
		/**
		 * 从comboBox中获取item的index
		 * **/
		public function getComboxIndexByItem(list:ArrayCollection,target:Object):int{
			for(var i:int=0;i<list.length;i++){
				if(equals(list[i],target)){
					return i;
				}
			}			
			return -1;
		}
		/***
		 * 比较两个实体
		 * **/
		public  function equals(obj:Object,target:Object):Boolean{		
			var xml:XML = describeType(obj);
			var properties:XMLList = xml..accessor.(@access != "writeonly")+xml..variable;
			var flag:Boolean = true;
			for(var j:uint=0;j<properties.length();j++){
				var property:String = properties[j].@name;
				if(property!="rowStatus" && property!="recordVersion" && property!="updatedDate"){
					if(obj[property]!=null && target[property]!=null){
						if(obj[property].toString()!=target[property].toString()){
							flag = false;
							break;
						}
					}else if(obj[property]!=target[property]){
						flag = false;
						break;
					}
				}
			}
			return flag;
		}
		
	}
}